package com.lzh.event.listener;


import com.lzh.event.UserOnlineEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
public class UserOnlineEventListener {

    // BEFORE_COMMIT, 事务提交前 ，可以保证两个都成功和失败
    //    AFTER_COMMIT, 事务提交后，可以使得前面逻辑有事务注解，但是后面事件没有成功也不会导致事务回滚
    //    AFTER_ROLLBACK, 事务回滚后
    //    AFTER_COMPLETION; 事务完成后

    @Async
    @TransactionalEventListener(value = UserOnlineEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    public void saveRedisAndPush(UserOnlineEvent event){
        log.info("saveRedisAndPush ==>");
    }



    @Async
    @TransactionalEventListener(UserOnlineEvent.class)
    public void saveDB(UserOnlineEvent event){
        log.info("saveDB ==>");
    }
}
